import Vue from "vue"
import Vuex from "vuex"
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

import modules from './modules'
const config = {
    // 本地持久化
    plugins: [createPersistedState()],

    // 数据源，相当于组件中的data
    state:{
        reuslt: 0,
        user:"张三",
        eat:"吃烧烤"
    },
    // 相当于计算属性，便于多个组件使用，相当于组件中的computed
    getters:{
        bigResult(state){
            return state.reuslt*10
        }
    },

    mutations:{
        ADD(state,payload){
            state.reuslt += payload
        },
        DEL(state,payload){
            state.reuslt -= payload
        },
        ADDODD(state,payload){
            state.reuslt += payload
        },
    },

    actions:{
        add(context,payload){
            context.commit('ADD',payload)
        },
        del(context,payload){
            context.commit('DEL',payload)
        },
        addOdd(context,payload){
            if (context.state.reuslt % 2) {
                context.commit('ADDODD',payload)
            }
        },
        addDelay(context,payload){
            setTimeout(() => {
                context.commit('ADD',payload)
            }, 1000);
        },
    },
    modules:modules

}

export default new Vuex.Store(config)